﻿using System;
using System.Drawing;
using System.IO;
using PokemonBattle.PokemonData.Custom;
using PokemonBattle.PokemonData.Rnd;
using PokemonBattle.PokemonData.Rom;
using PokemonBattle.PokemonData.Util;
using PokemonBattle.PokemonData.SQLite.DAL;

namespace PokemonBattle.PokemonData
{
    public static class BattleData
    {
        private static IDataProvider _dataProvider;

        private const string DATA_FILE = "data.pgd";
        private const string IMG_FILE = "images.pgd";

        private static DataService data;
        private static ImageService image;
        //private static ImageService customImage;

        static BattleData()
        {
            Load(AppDomain.CurrentDomain.BaseDirectory);
        }

        private static bool Load(string dir)
        {
            string datafile = Path.Combine(dir, DATA_FILE);
            string imgfile = Path.Combine(dir, IMG_FILE);

            try
            {
                //暂时不用 因为MainForm占用
                //image = new ImageService();
                //image.LoadData(imgfile);
                data = DataStreamHelper.FromFile<DataService>(datafile);
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public static MoveData GetMoveById(int id)
        {
            return data.Moves[id];
        }

        #region IDataProvider

        public static bool CheckPokemon(PokemonCustomInfo pokemon)
        {
            //if (_dataProvider == null)
            //{
            //    return false;
            //}
            //return _dataProvider.CheckPokemon(pokemon);
            return PokemonValidator.CheckPokemon(pokemon);
        }

        public static MoveData[] GetAllMoves()
        {
            //if (_dataProvider == null)
            //{
            //    return null;
            //}
            //return _dataProvider.GetAllMoves();
            return data.Moves.ToArray();
        }

        public static PokemonData[] GetAllPokemons()
        {
            //if (_dataProvider == null)
            //{
            //    return null;
            //}
            //return _dataProvider.GetAllPokemons();
            return data.Pokemons.ToArray();
        }

        public static Type[] GetAllTypes()
        {
            //if (_dataProvider == null)
            //{
            //    return null;
            //}
            //return _dataProvider.GetAllTypes();
            return data.Types.ToArray();
        }

        public static Bitmap GetImage(int identity, long position)
        {
            if (_dataProvider == null)
            {
                return null;
            }
            return _dataProvider.GetImage(identity, position);
#warning 无自定义
            return image.GetImage(position);
        }

        public static MoveData GetMove(string name)
        {
            //if (_dataProvider == null)
            //{
            //    return null;
            //}
            //return _dataProvider.GetMoveData(name);
            return data.Moves.Find(m => m.Name == name);
        }

        public static PokemonData GetPokemon(int identity)
        {
            //if (_dataProvider == null)
            //{
            //    return null;
            //}
            //return _dataProvider.GetPokemonData(identity);
            return DALPokemonData.GetModel(identity);
            //return data.Pokemons.Find(p => p.Identity == identity);
        }

        public static TeamData GetRandomTeam(Random random)
        {
            //if (_dataProvider == null)
            //{
            //    return null;
            //}
            //return _dataProvider.GetRandomTeam(random);
            //return RandomData.GetTeam(random);
            return RandomData.GetSmogonTeam(random);
        }

        public static Type GetTypeData(string name)
        {
            //if (_dataProvider == null)
            //{
            //    return null;
            //}
            //return _dataProvider.GetTypeData(name);
            return data.Types.Find(t => t.Name == name);
        }

        public static bool PokemonIsRemoved(int identity)
        {
            if (_dataProvider == null)
            {
                return false;
            }
            return _dataProvider.PokemonIsRemoved(identity);
        }

        public static CustomDataInfo CustomData
        {
            get
            {
                if (_dataProvider == null)
                {
                    return null;
                }
                return _dataProvider.CustomData;
            }
        }

        public static IDataProvider DataProvider
        {
            get
            {
                return _dataProvider;
            }
            set
            {
                _dataProvider = value;
            }
        }

        #endregion
    }
}